//
//  DYMessageDetailVC.m
//  kafuMerchant
//
//  Created by 帝云科技 on 2018/9/19.
//  Copyright © 2018年 帝云科技. All rights reserved.
//

#import "DYMessageDetailVC.h"
#import <WebKit/WebKit.h>

@interface DYMessageDetailVC ()<WKNavigationDelegate>

@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UILabel *timeLabel;
@property (nonatomic,strong)WKWebView *webView;
@property (nonatomic,strong)UIScrollView *scrollView;

@end

@implementation DYMessageDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
-(void)dy_initData
{
    [super dy_initData];
    
    self.navigationItem.title=@"消息详情";
    
    self.titleLabel.text=@"卡福服务质量";
    self.timeLabel.text=@"2019-08-03 12:33:44";
    [self.webView loadHTMLString:@"卡福服务质量卡福服务质量卡福服务质量卡福服务质量卡福服务质量" baseURL:nil];
    
}
-(void)dy_initUI
{
    [super dy_initUI];
    
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.titleLabel];
    [self.scrollView addSubview:self.timeLabel];
    [self.scrollView addSubview:self.webView];
    
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    //修改字体颜色  #9098b8
    //    [ webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#8A8A8A'"completionHandler:nil];
    XHQHUDHIDE(self.view);
    [webView evaluateJavaScript:@"document.body.scrollHeight"
              completionHandler:^(id result, NSError *_Nullable error) {
                  CGFloat height = [result floatValue];
                  self.webView.frame=CGRectMake(0,BILIHEIGHT(65), kScreenWidth,height);
                  self.scrollView.contentSize=CGSizeMake(0,self.webView.xhq_bottom);
              }];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    XHQHUDHIDE(self.view);
}
-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel=[Utils labelWithTitleFontSize:18 textColor:[UIColor xhq_aTitle] alignment:1];
        _titleLabel.frame=CGRectMake(BILIWIDTH(60), BILIHEIGHT(10), kScreenWidth-BILIWIDTH(120), BILIHEIGHT(20));
        _titleLabel.lineBreakMode=NSLineBreakByTruncatingTail;
    }
    return _titleLabel;
}

-(UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel=[Utils labelWithTitleFontSize:12 textColor:[UIColor xhq_assist] alignment:2];
        _timeLabel.frame=CGRectMake(kScreenWidth-BILIHEIGHT(200), BILIHEIGHT(40), BILIWIDTH(190), BILIHEIGHT(20));
    }
    return _timeLabel;
}


- (WKWebView *)webView
{
    if (!_webView)
    {
        WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
        WKUserContentController *userContent = [[WKUserContentController alloc]init];
        configuration.userContentController = userContent;
        //web 自适应屏幕宽度js
        NSString *jsString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';};";
        WKUserScript *script = [[WKUserScript alloc]initWithSource:jsString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        [userContent addUserScript:script];
        
        _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0,BILIHEIGHT(65+210), kScreenWidth, kScreenHeight-BILIHEIGHT(60+50)-kNavigationStatusHeight) configuration:configuration];
        _webView.navigationDelegate = self;
    }
    return _webView;
}
-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kNavigationStatusHeight-BILIHEIGHT(50))];
        _scrollView.alwaysBounceVertical=YES;
    }
    return _scrollView;
}
- (NSString *)htmlEntityDecode:(NSString *)string
{
    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    return string;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
